package com.triones.yjj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.system.domain.SysConfig;
import com.triones.yjj.entity.RegionDO;
import com.triones.yjj.entity.vo.RegionImportVo;
import com.triones.yjj.entity.vo.RegionVO;
import com.triones.yjj.mapper.RegionMapper;
import com.triones.yjj.service.RegionService;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.swing.BakedArrayList;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 地区 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class RegionServiceImpl implements RegionService {

    private final RegionMapper regionMapper;

    @Override
    public void saveBatch(List<RegionImportVo> volist) {
        for (RegionImportVo regionImportVo : volist) {
            String name = regionImportVo.getName();
            String[] split = name.split(",");
            RegionDO regionDO = new RegionDO();
            if (split.length == 2) {
                regionDO.setCode(regionImportVo.getCode());
                regionDO.setName(split[1]);
                regionDO.setLevel(1);
                regionDO.setParentId(0L);
                regionDO.setCreateTime(DateUtil.getDateTime());
                System.out.println(split[1] + " --> " + regionDO);
                regionMapper.insert(regionDO);
            } else if (split.length == 3) {
                if (split[1].equals(split[2])) {
                    regionDO.setCode(regionImportVo.getCode());
                    regionDO.setName(split[1]);
                    regionDO.setLevel(1);
                    regionDO.setParentId(0L);
                    regionDO.setCreateTime(DateUtil.getDateTime());
                    System.out.println(split[1] + " --> " + regionDO);
                    regionMapper.insert(regionDO);
                } else {
                    RegionDO selectOne = regionMapper.selectOne(new LambdaQueryWrapper<RegionDO>().eq(RegionDO::getName, split[1]));
                    regionDO.setCode(regionImportVo.getCode());
                    regionDO.setName(split[2]);
                    regionDO.setLevel(2);
                    regionDO.setParentId(selectOne.getId());
                    regionDO.setCreateTime(DateUtil.getDateTime());
                    System.out.println(split[2] + " --> " + regionDO);
                    regionMapper.insert(regionDO);
                }
            } else if (split.length == 4) {
                if (split[2].equals(split[3])) {
                    RegionDO selectOne = regionMapper.selectOne(new LambdaQueryWrapper<RegionDO>().eq(RegionDO::getName, split[1]));
                    regionDO.setCode(regionImportVo.getCode());
                    regionDO.setName(split[2]);
                    regionDO.setLevel(2);
                    regionDO.setParentId(selectOne.getId());
                    regionDO.setCreateTime(DateUtil.getDateTime());
                    System.out.println(split[2] + " --> " + regionDO);
                    regionMapper.insert(regionDO);
                } else {
                    RegionDO selectOne = regionMapper.selectOne(new LambdaQueryWrapper<RegionDO>().eq(RegionDO::getName, split[2]));
                    regionDO.setCode(regionImportVo.getCode());
                    regionDO.setName(split[3]);
                    regionDO.setLevel(3);
                    regionDO.setParentId(selectOne.getId());
                    regionDO.setCreateTime(DateUtil.getDateTime());
                    System.out.println(split[3] + " --> " + regionDO);
                    regionMapper.insert(regionDO);
                }
            }
        }
    }

    @Override
    public List<RegionVO> list() {
        LambdaQueryWrapper<RegionDO> lqw = new LambdaQueryWrapper<>();
        return regionMapper.selectVoList(lqw);
    }
}
